package com.lm.picocli.example;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.Callable;

@Command(name = "filetool", version = "FileTool 1.0",
        description = "一个用于文件操作的命令行工具",
        mixinStandardHelpOptions = true)
public class FileTool implements Callable<Integer> {

    @Option(names = {"-p", "--path"}, description = "文件路径", required = true)
    private String filePath;

    @Option(names = {"-c", "--count-lines"}, description = "统计文件行数")
    private boolean countLines;

    @Option(names = {"-r", "--read-content"}, description = "读取并输出文件内容")
    private boolean readContent;

    @Override
    public Integer call() throws Exception {
        if (countLines) {
            long lineCount = Files.lines(Paths.get(filePath)).count();
            System.out.println("文件行数: " + lineCount);
        }

        if (readContent) {
            String content = new String(Files.readAllBytes(Paths.get(filePath)));
            System.out.println("文件内容:\n" + content);
        }

        return 0;
    }

    public static void main(String[] args) {
        int exitCode = new CommandLine(new FileTool()).execute(args);
        System.exit(exitCode);
    }
}
